<template>
  <div class="goods-comment">
    <!-- 评价头部 -->
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            v-for="(comment, i) in commentInfo.tags"
            :key="comment.id"
            href="javascript:;"
            :class="{ active: currentindex === i }"
            @click="changecurrentindex(i)"
            >{{ comment.title }}（{{ comment.tagCount }}）
          </a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a
        href="javascript:;"
        :class="{ active: reqParams.sortField === null }"
        @click="changesort(null)"
        >默认
      </a>
      <a
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'createTime' }"
        @click="changesort('createTime')"
      >
        最新
      </a>
      <a
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'praiseCount' }"
        @click="changesort('praiseCount')"
      >
        最热
      </a>
    </div>
    <!-- 评价列表 -->
    <div class="list" v-if="commentlist">
      <div class="item" v-for="item in commentlist.items" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ formatnickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i
              v-for="i in 5"
              :key="i"
              :class="[
                'iconfont',
                i < item.score ? 'icon-wjx01' : 'icon-wjx02'
              ]"
            ></i>
            <span class="attr">{{ formatspecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <!-- 图片组件 -->
          <GoodsCommentGoods
            v-if="item.pictures"
            :pictures="item.pictures"
          ></GoodsCommentGoods>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <XtxPagination
      :total="commentInfo.items && commentInfo.items.length"
      :pagesize="reqParams.pageSize"
      @currentchange="currentchange"
    ></XtxPagination>
  </div>
</template>

<script>
import { ref, inject, reactive, watch } from 'vue'
import { findGoodsCommentInfo, findGoodsCommentList } from '@/apis/product'
import GoodsCommentGoods from './goods-comment-image.vue'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentGoods },
  setup () {
    // 评价信息
    const commentInfo = ref({})
    const goods = inject('goods')
    findGoodsCommentInfo(goods.value.id).then((res) => {
      // 先在数组前面追加两个
      res.result.tags.unshift({
        title: '有图',
        tagCount: res.result.hasPictureCount
      })
      res.result.tags.unshift({
        title: '全部评价',
        tagCount: res.result.evaluateCount
      })
      commentInfo.value = res.result
    })
    // 激活tag
    const currentindex = ref(0)
    // 改变当前tag
    const changecurrentindex = (i) => {
      currentindex.value = i
      const tag = commentInfo.value.tags[i]
      reqParams.page = 1
      if (tag.title === '全部') {
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (tag.title === '有图') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = null
        reqParams.tag = tag.title
      }
    }
    // 改变排序顺序
    const changesort = (name) => {
      reqParams.sortField = name
      reqParams.page = 1
    }
    // 准备筛选条件数据
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      // 排序方式：praiseCount 热度  createTime 最新
      sortField: null
    })
    const commentlist = ref([])
    // 筛选条件改变时发送请求
    watch(
      reqParams,
      () => {
        findGoodsCommentList(goods.value.id, reqParams).then((res) => {
          console.log(res.result)
          commentlist.value = res.result
        })
      },
      { immediate: true }
    )
    // 格式化数据
    const formatnickname = (name) => {
      return name.substr(0, 1) + '****' + name.substr(-1)
    }
    const formatspecs = (specs) =>
      specs.reduce((p, c) => `${p} ${c.name}：${c.nameValue}`, '').trim()
    // 页码改变时
    const currentchange = (current) => {
      reqParams.page = current
    }
    return {
      commentInfo,
      currentindex,
      reqParams,
      changecurrentindex,
      changesort,
      formatnickname,
      commentlist,
      formatspecs,
      currentchange
    }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
